package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.enums.BorrowerStatusEnum;
import com.atguigu.srb.core.enums.IntegralEnum;
import com.atguigu.srb.core.mapper.*;
import com.atguigu.srb.core.pojo.entity.*;
import com.atguigu.srb.core.pojo.vo.BorrowerApprovalVO;
import com.atguigu.srb.core.pojo.vo.BorrowerAttachVO;
import com.atguigu.srb.core.pojo.vo.BorrowerDetailVO;
import com.atguigu.srb.core.pojo.vo.BorrowerVO;
import com.atguigu.srb.core.service.BorrowerService;
import com.atguigu.srb.core.service.DictService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 借款人 服务实现类
 * </p>
 *
 * @author AtLong
 * @since 2022-06-11
 */
@Slf4j
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class BorrowerServiceImpl extends ServiceImpl<BorrowerMapper, Borrower> implements BorrowerService {

    @Resource
    private BorrowerMapper borrowerMapper;
    @Resource
    private DictService dictService;
    @Resource
    private UserIntegralMapper userIntegralMapper;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private BorrowerAttachMapper borrowerAttachMapper;

    //  NO_AUTH(0, "未认证"),
    //  AUTH_RUN(1, "认证中"),
    //  AUTH_OK(2, "认证成功"),
    //  AUTH_FAIL(-1, "认证失败"),

    // 保存借款人信息
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveBorrowerVOByUserId(BorrowerVO borrowerVO, Long userId) {
        // 查询用户会员信息
        UserInfo userInfo = userInfoMapper.selectById(userId);
        // 保存借款人信息
        Borrower borrower = new Borrower();
        BeanUtils.copyProperties(borrowerVO, borrower);
        borrower.setUserId(userId);
        borrower.setName(userInfo.getName());
        borrower.setIdCard(userInfo.getIdCard());
        borrower.setMobile(userInfo.getMobile());
        // 借款人信息正在进行认证
        borrower.setStatus(BorrowerStatusEnum.AUTH_RUN.getStatus());
        borrowerMapper.insert(borrower);

        // 保存借款人附件
        List<BorrowerAttach> borrowerAttachList = borrowerVO.getBorrowerAttachList();
        for (BorrowerAttach borrowerAttach : borrowerAttachList) {
            borrowerAttach.setBorrowerId(borrower.getId());
            borrowerAttachMapper.insert(borrowerAttach);
        }
        // 更新会员状态, 更新为认证中
        userInfo.setBorrowAuthStatus(BorrowerStatusEnum.AUTH_RUN.getStatus());
        userInfoMapper.updateById(userInfo);
    }

    // 获取用户的状态
    @Override
    public Integer getBorrowerStatus(Long userId) {
        // 构建查询条件
        QueryWrapper<Borrower> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        // 调用方法查询
        Borrower borrower = borrowerMapper.selectOne(queryWrapper);
        // 判断借款人是否已经提交
        if (ObjectUtils.isEmpty(borrower)) {
            return BorrowerStatusEnum.NO_AUTH.getStatus();
        }
        // 返回查询结果
        Integer status = borrower.getStatus();
        return status;
    }

    @Override
    public BorrowerDetailVO getDetailByBorrowerId(Long borrowerId) {
        // 查询借款人信息
        Borrower borrower = borrowerMapper.selectById(borrowerId);
        log.info("查询借款人信息--->>>" + borrower);
        // 封装借款人的基本信息
        BorrowerDetailVO borrowerDetailVO = new BorrowerDetailVO();
        BeanUtils.copyProperties(borrower, borrowerDetailVO);
        // 婚否和性别
        borrowerDetailVO.setMarry(borrower.getMarry() ? "已婚" : "未婚");
        borrowerDetailVO.setSex(borrower.getSex() == 0 ? "女" : "男");

        // 封装从数据字典获取赋值给下拉菜单列表
        String education = dictService.getNameByParentDictCodeAndValue("education", borrower.getEducation());
        borrowerDetailVO.setEducation(education);
        String industry = dictService.getNameByParentDictCodeAndValue("industry", borrower.getIndustry());
        borrowerDetailVO.setIndustry(industry);
        String returnSource = dictService.getNameByParentDictCodeAndValue("returnSource", borrower.getReturnSource());
        borrowerDetailVO.setReturnSource(returnSource);
        String relation = dictService.getNameByParentDictCodeAndValue("relation", borrower.getContactsRelation());
        borrowerDetailVO.setContactsRelation(relation);
        String income = dictService.getNameByParentDictCodeAndValue("income", borrower.getIncome());
        borrowerDetailVO.setIncome(income);

        // 审批状态封装
        String status = BorrowerStatusEnum.getMsgByStatus(borrower.getStatus());
        borrowerDetailVO.setStatus(status);

        //  获取附件Vo列表进行封装
        QueryWrapper<BorrowerAttach> borrowerAttachQueryWrapper = new QueryWrapper<>();
        borrowerAttachQueryWrapper.eq("borrower_id", borrowerId);
        List<BorrowerAttach> borrowerAttachList = borrowerAttachMapper.selectList(borrowerAttachQueryWrapper);

        ArrayList<BorrowerAttachVO> list = new ArrayList<>();
        for (BorrowerAttach borrowerAttach : borrowerAttachList) {
            BorrowerAttachVO borrowerAttachVO = new BorrowerAttachVO();
            borrowerAttachVO.setImageType(borrowerAttach.getImageType());
            borrowerAttachVO.setImageUrl(borrowerAttach.getImageUrl());
            list.add(borrowerAttachVO);
        }
        borrowerDetailVO.setBorrowerAttachVOList(list);
        // 返回数据
        return borrowerDetailVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void getApproval(BorrowerApprovalVO borrowerApprovalVO) {
        // 进行借款人Id,设置借款人审核状态
        Long borrowerId = borrowerApprovalVO.getBorrowerId();
        Borrower borrower = borrowerMapper.selectById(borrowerId);
        Integer status = borrowerApprovalVO.getStatus();
        borrower.setStatus(status);
        borrowerMapper.updateById(borrower);

        // 添加用户表的积分 获取用户id 获取用户对象 用户的原始积分
        Long userId = borrower.getUserId();
        UserInfo userInfo = userInfoMapper.selectById(userId);
        Integer integralOrigin = userInfo.getIntegral();
        // 计算积分
        // 添加user_integral用户基本信息积分
        UserIntegral userIntegral = new UserIntegral();
        userIntegral.setUserId(userId);
        userIntegral.setIntegral(borrowerApprovalVO.getInfoIntegral());
        userIntegral.setContent("借款人基本信息");
        userIntegralMapper.insert(userIntegral);
        Integer integralCurrent = integralOrigin + borrowerApprovalVO.getInfoIntegral();
        // 身份证积分 身份证信息是否正确
        if (borrowerApprovalVO.getIsIdCardOk()) {
            userIntegral = new UserIntegral();
            userIntegral.setUserId(userId);
            userIntegral.setIntegral(IntegralEnum.BORROWER_IDCARD.getIntegral());
            userIntegral.setContent(IntegralEnum.BORROWER_IDCARD.getMsg());
            userIntegralMapper.insert(userIntegral);
            integralCurrent += IntegralEnum.BORROWER_IDCARD.getIntegral();
        }

        // 房产积分  房产信息是否正确
        if (borrowerApprovalVO.getIsHouseOk()) {
            userIntegral = new UserIntegral();
            userIntegral.setUserId(userId);
            userIntegral.setIntegral(IntegralEnum.BORROWER_HOUSE.getIntegral());
            userIntegral.setContent(IntegralEnum.BORROWER_HOUSE.getMsg());
            userIntegralMapper.insert(userIntegral);
            integralCurrent += IntegralEnum.BORROWER_HOUSE.getIntegral();
        }

        // 车辆积分 车辆信息是否正确
        if (borrowerApprovalVO.getIsIdCardOk()) {
            userIntegral = new UserIntegral();
            userIntegral.setUserId(userId);
            userIntegral.setIntegral(IntegralEnum.BORROWER_CAR.getIntegral());
            userIntegral.setContent(IntegralEnum.BORROWER_CAR.getMsg());
            userIntegralMapper.insert(userIntegral);
            integralCurrent += IntegralEnum.BORROWER_CAR.getIntegral();
        }

        // 修改审核状态  计算算入总积分
        userInfo.setStatus(borrowerApprovalVO.getStatus());
        userInfo.setIntegral(integralCurrent);
        // 调用更新方法修改用户积分信息
        userInfoMapper.updateById(userInfo);

    }
}
